package com.yixin.hospital.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.bo.TemplateDetail;
import com.yixin.hospital.entity.domain.Template;
import com.yixin.hospital.entity.dto.req.template.TemplateAddReqDTO;
import com.yixin.hospital.entity.vo.ListTemplateVO;
import com.yixin.hospital.mapper.TemplateMapper;
import com.yixin.hospital.service.JobOfficeService;
import com.yixin.hospital.service.TemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【template(科室模板表)】的数据库操作Service实现
* @createDate 2024-04-25 15:41:24
*/
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template>
    implements TemplateService{

    @Resource
    private JobOfficeService jobOfficeService;

    @Override
    public ApiResult<List<ListTemplateVO>> allList(Long jobOfficeId) {
        LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(jobOfficeId)) {
            queryWrapper.eq(Template::getJobOffice, jobOfficeId);
        }
        List<Template> templateList = this.list(queryWrapper);
        List<ListTemplateVO> list = new ArrayList<>();
        for (Template template : templateList) {
            ListTemplateVO listTemplateVO = new ListTemplateVO();
            listTemplateVO.setId(template.getId());
            listTemplateVO.setName(template.getName());
            listTemplateVO.setJobOffice(template.getJobOffice());
            listTemplateVO.setJobOfficeName(jobOfficeService.getById(template.getJobOffice()).getName());
            List<TemplateDetail> templateDetailList = new ArrayList<>();
            templateDetailList.addAll(JSON.parseObject(template.getDetail().toString(), new TypeReference<List<TemplateDetail>>() {
            }.getType()));
            listTemplateVO.setTemplateDetailList(templateDetailList);
            list.add(listTemplateVO);
        }
        return ApiResult.success(list);
    }

    @Override
    public ApiResult<?> add(TemplateAddReqDTO templateAddReqDTO, String userName) {
        // 校验
        for (TemplateDetail templateDetail : templateAddReqDTO.getTemplateDetailList()) {
            if (StringUtils.isBlank(templateDetail.getColumn()) || templateDetail.getType() == null ||
                    templateDetail.getRequired() == null || (templateDetail.getType() == 2 && templateDetail.getTypeValues() == null) ) {
                return ApiResult.error("数据异常，请检查");
            }
        }
        // 添加
        if (templateAddReqDTO.getId() == null) {
            Template template = new Template();
            template.setJobOffice(templateAddReqDTO.getJobOffice());
            template.setName(templateAddReqDTO.getName());
            template.setDetail(new Gson().toJson(templateAddReqDTO.getTemplateDetailList()));
            template.setCreatedBy(userName);
            template.setUpdatedBy(userName);
            this.save(template);
            return ApiResult.success();
        }
        Template template = this.getById(templateAddReqDTO.getId());
        template.setJobOffice(templateAddReqDTO.getJobOffice());
        template.setName(templateAddReqDTO.getName());
        template.setDetail(new Gson().toJson(templateAddReqDTO.getTemplateDetailList()));
        template.setUpdatedBy(userName);
        this.updateById(template);
        return ApiResult.success();
    }
}




